﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Data.SqlClient;
using System.Collections.Generic;

public partial class controls_client_rate : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void LoadRate(int id)
    {
        List<SqlParameter> param = new List<SqlParameter>();
        param.Add(new SqlParameter("SanPham", id));
        DataTable dt = ExcuteSQL.Store("DanhGiaSanPham_ById", param, CommandType.StoredProcedure);        
        if (dt.Rows.Count > 0)
        {
            DataRow dr = dt.Rows[0];
            // quality
            double good = Convert.ToInt32(dr["CLTot"].ToString());
            double normal = Convert.ToInt32(dr["CLBinhThuong"].ToString());
            double bad = Convert.ToInt32(dr["CLChuaTot"].ToString());
            double count = good + normal + bad;
            double pcGood = good / count * 100;
            double pcNormal = normal / count * 100;
            double pcBad = 100 - (pcGood + pcNormal);
            rCount.Text = "Số lượt đánh giá " + count;
            // bind data
            rqGood.Text = pcGood.ToString("F0") + "%";
            rqNormal.Text = pcNormal.ToString("F0") + "%";
            rqBad.Text = pcBad.ToString("F0") + "%";

            // price
            good = Convert.ToInt32(dr["GHopLy"].ToString());
            normal = Convert.ToInt32(dr["GBinhThuong"].ToString());
            bad = Convert.ToInt32(dr["GCao"].ToString());
            count = good + normal + bad;
            pcGood = good / count * 100;
            pcNormal = normal / count * 100;
            pcBad = 100 - (pcGood + pcNormal);
            // bind data
            rpGood.Text = pcGood.ToString("F0") + "%";
            rpNormal.Text = pcNormal.ToString("F0") + "%";
            rpBad.Text = pcBad.ToString("F0") + "%";
        }
        // quality
        qGood.Width = Unit.Parse(rqGood.Text);
        qNormal.Width = Unit.Parse(rqNormal.Text);
        qBad.Width = Unit.Parse(rqBad.Text);

        // price
        pGood.Width = Unit.Parse(rpGood.Text);
        pNormal.Width = Unit.Parse(rpNormal.Text);
        pBad.Width = Unit.Parse(rpBad.Text);
    }

    public void UpdateRate(int id)
    {
        try
        {
            int qGood = 0, qNormal = 0, qBad = 0;
            int pGood = 0, pNormal = 0, pBad = 0;
            #region get rate values
            switch (cboQuality.SelectedItem.Value)
            {
                case "3":
                    qGood = 1;
                    break;
                case "2":
                    qNormal = 1;
                    break;
                case "1":
                    qBad = 1;
                    break;
                default:                    
                    break;
            }            
            switch (cboPrice.SelectedItem.Value)
            {
                case "3":
                    pGood = 1;
                    break;
                case "2":
                    pNormal = 1;
                    break;
                case "1":
                    pBad = 1;
                    break;
                default:
                    break;
            }
            #endregion
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("SanPham", id));
            param.Add(new SqlParameter("CLTot", qGood));
            param.Add(new SqlParameter("CLBinhThuong", qNormal));
            param.Add(new SqlParameter("CLChuaTot", qBad));
            param.Add(new SqlParameter("GHopLy", pGood));
            param.Add(new SqlParameter("GBinhThuong", pNormal));
            param.Add(new SqlParameter("GCao", pBad));
            if (ExcuteSQL.Query("DanhGiaSanPham_Update", param, CommandType.StoredProcedure))
            {
                LoadRate(id);
                ViewResult();
            }
        }
        catch (Exception ex)
        { }
    }
    public void ViewResult()
    {
        pRate.Visible = false;
        pResult.Visible = true;
    }
    public void ViewRate()
    {
        pRate.Visible = true;
        pResult.Visible = false;
    }
}
